package com.maoye.ossp.biz.brand.controller;

import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.brand.entity.Brand;
import com.maoye.ossp.biz.brand.service.IBrandLevelService;
import com.maoye.ossp.biz.brand.service.IBrandService;
import com.maoye.ossp.biz.brand.utils.BrandUtils;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;

/**
 * 对象功能:品牌基本信息 控制器类
 * 开发公司:maoye shenzhen
 * 开发人员:harry
 * 创建时间:2013-09-30 11:37:20
 */
@Controller
@RequestMapping("/biz/brand/brand/")
public class BrandController extends BaseController
{
	@Resource
	private IBrandService brandService;
	@Resource
	private IBrandLevelService brandLevelService;
	
	/**
	 * 添加或更新品牌基本信息
	 * @param brandLevel
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "save")
	public String save(Brand brand, Model model,
			RedirectAttributes redirectAttributes) 
	{
		try
		{
			String msg = brandService.saveData(brand);
			if(null != msg && !"".equals(msg))
			{
				addMessage(redirectAttributes, msg);
				model.addAttribute("brand", brand);
				return "/biz/brand/brand/add";
			}
			addMessage(redirectAttributes, "保存成功");
		}
		catch(Exception e)
		{
			addMessage(redirectAttributes, "保存数据失败");
		}
		return "redirect:/biz/brand/brand/list";
	}
	
	/**
	 * 取得品牌基本信息分页列表
	 * @param brand
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "list")
	public String list(Brand brand, HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception
	{
		QueryFilter queryFilter = getQuerFilter(request, brand);
		Page<Brand> page = brandService.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/biz/brand/brand/list", model, request);
	}
	
	@RequestMapping(value = "add")
	public String add(Brand brand, Model model)  throws Exception 
	{
		model.addAttribute("brand", brand);
		return "/biz/brand/brand/add";
	}
	
	@RequestMapping(value = "{id}/edit")
	public String edit(@PathVariable String id, Model model) throws Exception 
	{
		Brand brand = brandService.get(Long.valueOf(id));
		model.addAttribute("brand", brand);
		return "/biz/brand/brand/add";
	}

	@RequestMapping(value = "{id}/view")
	public String view(@PathVariable String id, Model model) 
	{
		Brand brand = brandService.get(Long.valueOf(id));
		model.addAttribute("brand", brand);
		return "/biz/brand/brand/view";
	}
	
	@RequestMapping(value = "{id}/delete")
	public String delete(@PathVariable String id, Model model,
			RedirectAttributes redirectAttributes) 
	{
		Long[] lAryId = {Long.valueOf(id)};
		try {
			brandService.delAll(lAryId);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/brand/list";
	}
	
	@RequestMapping(value = "deleteAll")
	public String deleteAll(HttpServletRequest request,
			HttpServletResponse response, Model model,RedirectAttributes redirectAttributes) 
	{
		String deleteIds = request.getParameter("deleteIds");
		Long[] lAryId = BrandUtils.getLongAryByStr(deleteIds,";");
		try {
			brandService.delAll(lAryId);
			
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		addMessage(redirectAttributes, "删除数据成功！");
		return "redirect:/biz/brand/brand/list";
	}
	
	/**
	 * 自动补全品牌中文名称
	 * @param request   
	 * @param response
	 * @return List
	 * @throws Exception
	 */
	@RequestMapping(value="autocompleteBrandName")
	public void autocompleteBrandName(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String param = request.getParameter("q");
		if(null == param || "".equals(param))
		{
			param = null;
		}else
		{
			param = param.replace("'", "''").trim();
		}
		
		//String param = new String(key.getBytes("iso8859-1"),"UTF-8");
		String name = request.getParameter("name");
		String paramFlag  = name.replace("'", "''").trim();
		String strResult = brandService.autoCompleteBrandName(param, paramFlag);
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		writer.print(strResult);
	}
}
